<template>
  <el-dialog @close="close" v-model="dialogVisible" :title="title" width="50%">
    <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="110px">
      <el-form-item label="医院平台" prop="platform_id" v-if="!route.query.id">
        <el-select
          v-model="ruleForm.platform_id"
          clearable
          filterable
          placeholder="请选择"
          style="width: 100%"
          @change="handlePlatformIdChange"
        >
          <el-option v-for="item in platList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="科室" prop="department_id">
        <el-select
          v-model="ruleForm.department_id"
          clearable
          filterable
          placeholder="请选择"
          style="width: 100%"
        >
          <el-option v-for="item in deptList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="ruleForm.name"
          placeholder="请输入名称"
          :disabled="ruleForm.id ? true : false"
        />
      </el-form-item>
      <el-form-item label="紧急联系电话" prop="mobile">
        <el-input
          v-model="ruleForm.mobile"
          placeholder="请输入联系电话"
          :disabled="ruleForm.id ? true : false"
        />
      </el-form-item>
      <el-form-item label="管理权限" prop="admintype">
        <el-radio-group v-model="ruleForm.admintype" class="ml-4">
          <el-radio-button label="0">无</el-radio-button>
          <el-radio-button label="1">科室权限</el-radio-button>
          <el-radio-button label="2">平台权限</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否添加会员" prop="isUser">
        <el-switch
          v-model="ruleForm.isUser"
          inline-prompt
          active-text="启用"
          inactive-text="禁用"
          :active-value="1"
          :inactive-value="0"
          :disabled="ruleForm.isUser && ruleForm.userid ? true : false"
        ></el-switch>
      </el-form-item>
      <el-form-item label="密码" prop="password" v-if="ruleForm.isUser == 1">
        <el-input v-model="ruleForm.password" placeholder="请输入密码" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleClose(ruleFormRef)">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import { ElMessageBox, ElMessage, FormInstance, ElNotification } from 'element-plus'
import { reactive, ref } from 'vue'
import { departmentList, addPersonnel, editPersonnel, platformList } from '@/api/admin'
import { useRoute } from 'vue-router'
const route = useRoute()
const ruleFormRef = ref<FormInstance>()
const dialogVisible = ref<boolean>(false)
const title = ref('新增人员')

const rules = reactive({
  platform_id: [{ required: true, message: '请选择医院平台', trigger: 'change' }],
  department_id: [{ required: true, message: '请选择科室', trigger: 'change' }],
  name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
  mobile: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
})

const ruleForm = reactive({
  platform_id: Number(route.query.id) || '',
  department_id: Number(route.query.deptId) || '',
  name: '',
  mobile: '',
  status: 1,
  admintype: '0',
  isUser: 0,
  password: '',
  userid: '',
})
const deptList = ref() //部门
const platList = ref() //医院平台
const saveLoading = ref(false)
const emit = defineEmits<{ (event: 'refresh'): void }>()

function close() {
  rules.password[0].required = true
  rules.password[0].message = '请输入密码'
  ruleFormRef.value.resetFields()
  if (ruleForm.id) delete ruleForm.id
  Object.keys(ruleForm).forEach((key) => {
    if (key == 'platform_id') ruleForm[key] = Number(route.query.id) || ''
    else if (key == 'department_id') ruleForm[key] = Number(route.query.deptId) || ''
    else if (key == 'status') ruleForm[key] = 1
    else if (key == 'admintype') ruleForm[key] = '0'
    else if (key == 'isUser') ruleForm[key] = 0
    else ruleForm[key] = ''
  })
}

const show = (item) => {
  title.value = '新增人员'
  if (item && item.id) {
    title.value = '编辑人员'
    ruleForm.id = item.id
    Object.keys(ruleForm).forEach((key) => {
      ruleForm[key] = item[key]
    })
    if (item.userid == null || item.userid == 0) ruleForm.isUser = 0
    else ruleForm.isUser = 1
    rules.password[0].required = false
    rules.password[0].message = '不填写默认不修改'
  }
  dialogVisible.value = true
  if (route.query.id) {
    getDeptList()
  } else {
    getPlatList()
  }
}

//医院平台列表
const getPlatList = () => {
  platformList({ selectFields: 'id,name' }).then((res) => {
    if (res.code == 200) {
      platList.value = res.data
      if(res.data.length==1) route.query.id = ruleForm.platform_id = res.data[0].id;
    }
  })
}

//上级部门列表
const getDeptList = () => {
  let filter = JSON.stringify({ parentid: 0 })
  departmentList({
    platform_id: ruleForm.platform_id,
    selectFields: 'id,name',
    filter: filter,
  }).then((res) => {
    if (res.code == 200) {
      deptList.value = res.data
    }
  })
}

const handlePlatformIdChange = () => {
  ruleForm.department_id = ''
  deptList.value = []
  getDeptList()
}

const handleClose = async (done: () => void) => {
  await ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      saveLoading.value = true
      if (ruleForm.id) {
        //编辑
        editPersonnel(ruleForm).then((res) => {
          if (res.code == 200) {
            ElNotification({
              title: '编辑成功',
              message: res.msg,
              type: 'success',
            })
            emit('refresh')
            dialogVisible.value = false
          }
          saveLoading.value = false
        })
      } else {
        //新增
        addPersonnel(ruleForm).then((res) => {
          if (res.code == 200) {
            ElNotification({
              title: '添加成功',
              message: res.msg,
              type: 'success',
            })
            emit('refresh')
            dialogVisible.value = false
          }
          saveLoading.value = false
        })
      }
    }
  })
}

defineExpose({
  show,
})
</script>
<style lang="scss" scoped>
</style>

